/**
 * 2015-12-18
 * StudentDaoImpl.java
 * author:sherlochao
 */
package com.sherlochao.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.sherlochao.dao.StudentDao;
import com.sherlochao.model.Course;
import com.sherlochao.model.Student;
import com.sherlochao.model.StudyCourse;
import com.sherlochao.model.Teacher;

/**
 * @author yinchao
 *
 */
@Repository("studentDao")
public class StudentDaoImpl implements StudentDao{

	@Resource
	private SessionFactory sessionFactory;
	@Resource
	private StudentDao studentDao;
	
	public Session getSession() {
		return sessionFactory.openSession();
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	@Override
	public Student load(String id) {
		// TODO Auto-generated method stub
		return (Student)this.getSession().load(Student.class,id);
	}

	/**
	 * 点击查询学生所需要的方法
	 */
	//---------------------------------------------------------------------
	@Override
	public Student get(String id) {
		// 得到学生的个人信息
//		return (Student)this.getSession().get(Student.class,id);
		String hql = "from Student where id = ?";
		Query query = this.getSession().createQuery(hql);
		query.setString(0, id);
		return (Student) query.uniqueResult();
	}
	
	
	//返回该学生所选的所有课程
	@Override
	public List<StudyCourse> listStudyCourseByStudentId(String studentId){
		String hql = "from StudyCourse where student_id = ?";
		Query query = this.getSession().createQuery(hql);
		query.setString(0, studentId);
		List<StudyCourse> studyCourses = query.list();
		return studyCourses;
	}
	
	//返回该学生所选的课程的课程名
	@Override
	public List<Course> listCourseByStudentId(String studentId){
		List<StudyCourse> studyCourses = studentDao.listStudyCourseByStudentId(studentId);
		List<Course> courses = new ArrayList<Course>();
		
		for(int i = 0; i < studyCourses.size(); i++){
			String hql = "from Course where id = ? ";
			Query query = this.getSession().createQuery(hql);
			query.setString(0, studyCourses.get(i).getCourseId());
			courses.add((Course)query.uniqueResult());
		}
		return courses;

	}
	
	//返回该学生所对应课程的老师名
	@Override
	public List<Teacher> listTeacherNameByStudentId(String studentId){
		List<Course> courses = studentDao.listCourseByStudentId(studentId);
		List<Teacher> teachers = new ArrayList<Teacher>();
		for(int i = 0; i < courses.size(); i++){
			String hql = "from Teacher where id = ? ";
			Query query = this.getSession().createQuery(hql);
			query.setString(0, courses.get(i).getTeacherId());
			teachers.add((Teacher)query.uniqueResult());
		}
		return teachers;
	}
	//---------------------------------------------------------------------

	@Override
	public String save(Student entity) {
		// TODO Auto-generated method stub
		return (String)this.getSession().save(entity);
	}


	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		Student entity = this.load(id);
		this.getSession().delete(entity);
	}

	
	@Override
	public List<Student> findAllStudentByTeacherId(String teacherId){
		String hql = "from Student where teacher_id = ?";
		Query query = this.getSession().createQuery(hql);
		query.setString(0, teacherId);
		List<Student> studentList = query.list();
		return studentList;
	}
	

}
